package com.disktalk.web;

import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.joda.time.LocalTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.disktalk.entry.GroupUser;
import com.disktalk.service.GroupUserService;
import com.disktalk.service.HelloWorldService;

@Controller
public class WelcomeController {

	private final Logger logger = LoggerFactory.getLogger(WelcomeController.class);
	private final HelloWorldService helloWorldService;
    private final AtomicLong counter = new AtomicLong();
	@Autowired
	public WelcomeController(HelloWorldService helloWorldService) {
		this.helloWorldService = helloWorldService;
	}

	//直接返回String的jsp页面
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index(Map<String, Object> model) {
		LocalTime currentTime = new LocalTime();
		
		logger.debug("index() is executed!!!!");

		/*model.put("title", helloWorldService.getTitle(""));
		model.put("msg", counter.addAndGet(1) + 
				helloWorldService.getDesc() + 
				currentTime.toString());*/
		
		return new ModelAndView("redirect:/file/list");
	}

	//直接返回ModelAndView的jsp页面，单一参数
	@RequestMapping(value = "/hello/{name:.+}", method = RequestMethod.GET)
	public ModelAndView hello(@PathVariable("name") String name) {

		/*logger.debug("hello() is executed - $name {}", name);

		ModelAndView model = new ModelAndView();
		model.setViewName("index");
		
		model.addObject("title", helloWorldService.getTitle(name));
		model.addObject("msg", helloWorldService.getDesc());
		*/
		
		System.err.println("9999999999999999");
		return  new ModelAndView("redirect:/file/list?id=1");

	}
	
	//直接返回ModelAndView的jsp页面，多个参数
	@RequestMapping(value = "/helloMore", method = RequestMethod.GET)
	public ModelAndView helloMore(@RequestParam("id") int id,
			@RequestParam("title") String title,
			@RequestParam("msg") String msg) {
		
		logger.debug("helloMore() is executed - $title {}", title);

		ModelAndView model = new ModelAndView();
		model.setViewName("index");
		
		model.addObject("title", title);
		model.addObject("msg", msg);
		
		return model;
	}

	

}